﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace AusProject.OrderDataControls
{
    public partial class TableDataBox : AusProject.OrderDataControls.DataBox
    {
        protected override void SetDesignMode()
        {
            this.BackColor = SystemColors.ControlDark;

            foreach (DataBox dbox in panelTemplate.Controls)
            {
                dbox.Mode = DataControlMode.Design;
            }

            panelTemplate.Anchor = AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right | AnchorStyles.Bottom;
            this.AutoSize = false;
        }

        protected override void SetRunningMode()
        {
            this.BackColor = Color.Transparent;

            foreach (DataBox dbox in panelTemplate.Controls)
            {
                dbox.Mode = DataControlMode.Running;
            }

            panelTemplate.Anchor = AnchorStyles.Left | AnchorStyles.Top;
            this.AutoSize = true;
        }


        public override string ToString()
        {
            return "TableBox";
        }


        public TableDataBox()
        {
            InitializeComponent();
            Mode = DataControlMode.Design;
            this.MinimumSize = new Size(this.MinimumSize.Width, 30);
        }

        private void TableDataBox_Paint(object sender, PaintEventArgs e)
        {
            //if (mode == DataControlMode.Design)
            {
                e.Graphics.DrawRectangle(borderDashPen, new Rectangle(0, 0, this.Width - 1, this.Height - 1));
            }
        }

        private void TableDataBox_Resize(object sender, EventArgs e)
        {
        }

        private void TableDataBox_Load(object sender, EventArgs e)
        {
        }


        #region OrderForm DragDrop Ability

        public void StartDragDrop()
        {
            panelTemplate.AllowDrop = true;
        }

        DataBox newCreatedControl = null;

        private void TableDataBox_DragEnter(object sender, DragEventArgs e)
        {
            newCreatedControl = (TextDataBox)e.Data.GetData(typeof(TextDataBox));
            if (newCreatedControl == null)
            {
                newCreatedControl = (NumberDataBox)e.Data.GetData(typeof(NumberDataBox));
            }
            if (newCreatedControl == null)
            {
                newCreatedControl = (DateDataBox)e.Data.GetData(typeof(DateDataBox));
            }
            if (newCreatedControl == null)
            {
                newCreatedControl = (DropListDataBox)e.Data.GetData(typeof(DropListDataBox));
            }
            if (newCreatedControl == null)
            {
                newCreatedControl = (CheckDataBox)e.Data.GetData(typeof(CheckDataBox));
            }
            if (newCreatedControl == null)
            {
                newCreatedControl = (TickDataBox)e.Data.GetData(typeof(TickDataBox));
            }

            if (newCreatedControl != null)
            {
                e.Effect = DragDropEffects.Copy;
                panelTemplate.Controls.Add(newCreatedControl);
                newCreatedControl.Location = panelTemplate.PointToClient(new Point(e.X, e.Y));
            }
            else
            {
                e.Effect = DragDropEffects.None;
            }
        }

        private void TableDataBox_DragOver(object sender, DragEventArgs e)
        {
            if (newCreatedControl != null)
            {
                newCreatedControl.Location = panelTemplate.PointToClient(new Point(e.X, e.Y));
            }
        }

        private void TableDataBox_DragLeave(object sender, EventArgs e)
        {
            if (newCreatedControl != null)
            {
                panelTemplate.Controls.Remove(newCreatedControl);
                newCreatedControl.Location = new Point(0, 0);
                newCreatedControl = null;
            }
        }

        private void TableDataBox_DragDrop(object sender, DragEventArgs e)
        {
            InitNewControl(newCreatedControl);
        }

        #endregion


        public override DataBoxInfo ExportDataBoxInfo()
        {
            List<List<DataBoxInfo>> rows = new List<List<DataBoxInfo>>();

            foreach (Panel row in this.Controls)
            {
                List<DataBoxInfo> subBoxes = new List<DataBoxInfo>();
                foreach (DataBox dbox in row.Controls)
                {
                    subBoxes.Add(dbox.ExportDataBoxInfo());
                }
                rows.Add(subBoxes);
            }

            return new DataBoxInfo(-1, DataBoxType.TableDataBox, null, Editable, BorderMode, null, Left, Top, Width, Height, rows, -1, null, new DataBoxFont(this.Font.Name, this.Font.Size, this.Font.Bold, this.Font.Italic));
        }


                
        #region Pass subBoxes' events


        public event EventHandler SubBoxEnter;
        public event EventHandler SubBoxMove;
        public event EventHandler SubBoxResize;
        public event EventHandler SubBoxTitleChanged;


        public void AddNewControl(DataBox dbox)
        {
            panelTemplate.Controls.Add(dbox);
            InitNewControl(dbox);
        }

        public void AddNewControl(Panel panel, DataBox dbox)
        {
            panel.Controls.Add(dbox);
            InitNewControl(dbox);
        }

        public void InitNewControl(DataBox dbox)
        {
            dbox.Enter += new System.EventHandler(this.subBox_Enter);
            dbox.Move += new System.EventHandler(this.subBox_Move);
            dbox.Resize += new System.EventHandler(this.subBox_Resize);
            dbox.TitleChanged += new System.EventHandler(this.subBox_TitleChanged);
        }


        private void subBox_Enter(object sender, EventArgs e)
        {
            if (SubBoxEnter != null)
            {
                SubBoxEnter(sender, new EventArgs());
            }
        }

        private void subBox_Resize(object sender, EventArgs e)
        {
            if (SubBoxResize != null)
            {
                SubBoxResize(sender, new EventArgs());
            }
        }

        private void subBox_Move(object sender, EventArgs e)
        {
            if (SubBoxMove != null)
            {
                SubBoxMove(sender, new EventArgs());
            }
        }

        private void subBox_TitleChanged(object sender, EventArgs e)
        {
            if (SubBoxTitleChanged != null)
            {
                SubBoxTitleChanged(sender, new EventArgs());
            }
        }
        
        #endregion Pass subBoxes' events


        private void TableDataBox_MouseDown(object sender, MouseEventArgs e)
        {
            panelTemplate.Focus();
            this.OnEnter(null);
        }

        private void panelTemplate_MouseDown(object sender, MouseEventArgs e)
        {
            Point p = this.PointToClient(panelTemplate.PointToScreen(e.Location));
            this.OnMouseDown(new MouseEventArgs(e.Button, e.Clicks, p.X, p.Y, e.Delta));
        }

        private void panelTemplate_MouseMove(object sender, MouseEventArgs e)
        {
            Point p = this.PointToClient(panelTemplate.PointToScreen(e.Location));
            this.OnMouseMove(new MouseEventArgs(e.Button, e.Clicks, p.X, p.Y, e.Delta));
        }

        private void panelTemplate_MouseUp(object sender, MouseEventArgs e)
        {
            Point p = this.PointToClient(panelTemplate.PointToScreen(e.Location));
            this.OnMouseUp(new MouseEventArgs(e.Button, e.Clicks, p.X, p.Y, e.Delta));
        }


        public List<DataBoxInfo> TemplateDataBoxes { get; set; }


        private void addNewRowToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Panel lastRow = (Panel)this.Controls[this.Controls.Count - 1];

            Panel nRow = new Panel();
            nRow.Left = lastRow.Left;
            nRow.Top = lastRow.Top + lastRow.Height;
            nRow.Width = lastRow.Width;
            nRow.Height = lastRow.Height;
            nRow.MouseClick += panelTemplate_MouseClick;
            foreach (DataBoxInfo dboxInfo in TemplateDataBoxes)
            {
                DataBox nBox = dboxInfo.CreateDataBox();
                nBox.Mode = DataControlMode.Running;
                nRow.Controls.Add(nBox);
            }
            this.Controls.Add(nRow);

            this.Invalidate();
        }

        Panel selectedRow = null;

        private void panelTemplate_MouseClick(object sender, MouseEventArgs e)
        {
            if (Mode == DataControlMode.Running)
            {
                selectedRow = (Panel)sender;

                deleteThisRowToolStripMenuItem.Visible = this.Controls.Count > 1;

                if (e.Button == System.Windows.Forms.MouseButtons.Right)
                {
                    cmnuRows.Show(selectedRow.PointToScreen(e.Location));
                }
            }
        }

        private void deleteThisRowToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Controls.Remove(selectedRow);
            selectedRow = null;

            this.Controls[0].Top = 1;
            for (int i = 1; i < this.Controls.Count; i++)
            {
                this.Controls[i].Top = this.Controls[i - 1].Top + this.Controls[i - 1].Height;
            }

            this.Invalidate();
        }



        public Panel AddNewRow()
        {
            Panel lastRow = (Panel)this.Controls[this.Controls.Count - 1];

            Panel nRow = new Panel();
            nRow.Left = lastRow.Left;
            nRow.Top = lastRow.Top + lastRow.Height;
            nRow.Width = lastRow.Width;
            nRow.Height = lastRow.Height;
            nRow.MouseClick += panelTemplate_MouseClick;
            this.Controls.Add(nRow);

            return nRow;
        }

    }
}